<template>
  <div class="container" ref="mainCon">
    <topTitle :titleText="titleText"></topTitle>
    <div class="mainCon">
      <el-row class="toolbar searchInputWidth" style="padding-bottom: 0">
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="人员" class="row-padding-bottom">
              <el-input
                v-model="filterField.keyword"
                @blur="filterField.keyword = changeValue($event)"
                placeholder="请输入姓名或身份证号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="录入日期" class="row-padding-bottom">
              <el-date-picker
                v-model="date"
                type="daterange"
                unlink-panels
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="民族" class="row-padding-bottom">
                <el-select
                  v-model="filterField.nationality"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in nationOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <!-- <el-form-item label="区域" class="row-padding-bottom">
                        <el-cascader ref="manageArea" v-model="bizAreaId" :options="areasOptions" @change="getAreaId" clearable filterable :props="{ checkStrictly: true }"></el-cascader>
                    </el-form-item>
                    <el-form-item label="行业" class="row-padding-bottom">
                        <el-cascader ref="industry" v-model="industryId" :options="industryOptions" @change="getIndustryId" clearable filterable :props="{ checkStrictly: true }"></el-cascader>
                    </el-form-item> -->
              <el-form-item label="年龄" class="row-padding-bottom employeeAge">
                <el-input
                  type="primary"
                  placeholder="请输入"
                  @blur="filterField.ageStart = changeValue($event)"
                  v-model="filterField.ageStart"
                  style="width: 94px"
                ></el-input>
                <span style="margin: 0 10px">至</span>
                <el-input
                  type="text"
                  placeholder="请输入"
                  @blur="filterField.ageEnd = changeValue($event)"
                  v-model="filterField.ageEnd"
                  style="width: 94px"
                ></el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-col :lg="8" :xl="6">
            <el-form-item label="">
              <a
                @click="toggleAdvanced"
                style="margin-left: 40px; color: #409eff; cursor: pointer"
                class="btnColor"
              >
                {{ advanced ? '收起' : '展开' }}
                <i
                  :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                ></i>
              </a>
              <el-button
                type="primary"
                icon="icon iconfont iconchazhao1"
                @click="searchList"
                class="searchBtn"
              >
                <span>搜索</span>
              </el-button>
            </el-form-item>
            <el-form-item>
              <exportBtn
                :dataLen="tableList.length"
                :filterField="filterField"
                api="api-s/report/export_JC_NC_EMPLOYEE_BASIC"
              ></exportBtn>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="spacing"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column prop="name" label="人员" width="180">
            <template slot-scope="scope">
              <el-link
                type="primary"
                v-if="isShowDetail"
                @click="toEmployeeDetail(scope.$index, scope.row)"
                >{{ scope.row.name }}</el-link
              >
              <span v-else>{{ scope.row.name || '--' }}</span>
              <p>{{ scope.row.idcard || '--' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="inputdate"
            label="录入日期"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            min-width="150"
          ></el-table-column>
          <el-table-column
            prop="sexValue"
            label="性别"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="age"
            label="年龄"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="nationalityValue"
            label="民族"
            align="center"
            :formatter="formatTd"
          ></el-table-column>
          <el-table-column
            prop="birthday"
            label="出生日期"
            sortable="custom"
            align="center"
            :formatter="formatTd"
            min-width="140px"
          ></el-table-column>
          <el-table-column prop="address" label="家庭住址" width="230">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.address"
                :columnName="'address'"
              ></copyText>
            </template>
          </el-table-column>
          <!-- 表格数据为空时，页面显示 -->
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关人员数据"></emptyTable>
          </div>
        </el-table>
        <!-- filterField为搜索条件 -->
        <!-- getTableList为获取子组件的数据 -->
        <Pagination
          ref="pagination"
          @getpageSize="getpageSize"
          @getpageNo="getpageNo"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
        ></Pagination>
      </div>
    </div>
  </div>
</template>

<script>
import scrollTable from '@/mixin/scrollTable.js'
export default {
  name: 'monitorEmployee',
  mixins: [scrollTable],
  data() {
    return {
      titleText: '职工名册',
      advanced: false,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        },
      },
      nationOptions: [], //民族
      // areasOptions: [], //区域
      // industryOptions: [], //行业
      //表格数据
      tableList: [],
      requestUrl: 'api-s/nc/employeestats/base/basic',
      date: [],
      // bizAreaId: "", //区域
      // industryId: "", //行业
      //搜索条件
      filterField: {
        keyword: '', //姓名或身份证号
        nationality: '', //名族
        // bizAreaId: "", //区域
        // industryId: "", //行业
        createTimeEnd: '',
        createTimeStart: '',
        ageStart: '', //年龄
        ageEnd: '',
      },
      pageSize: 20,
      pageNo: 1,
      employeeId: '', //务工id
      isShow: false, //列表加载
      loading: true,
      isShowDetail: '',
    }
  },
  created() {
    // 民族
    this.getNation()
    //获取行业
    // this.selectIndustry();
    //获取区域
    // this.selectAreas();
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData()
    })
    this.isShowDetail =
      this.$store.state.buttonLimitsArr.indexOf('company:contract:detail') > -1
  },
  activated() {
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    getNation() {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/findbytype',
        data: {
          type: 'NATION',
        },
      })
        .then((response) => {
          this.nationOptions = response.data.result
        })
        .catch((error) => {
          this.$message.error('数据获取失败')
        })
    },

    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 搜索
    searchList() {
      if (this.date) {
        this.filterField.createTimeStart = this.date[0]
        this.filterField.createTimeEnd = this.date[1]
      } else {
        this.filterField.createTimeStart = ''
        this.filterField.createTimeEnd = ''
      }
      var ageStart = this.filterField.ageStart
      var ageEnd = this.filterField.ageEnd
      var re = /^([1-9]|[0-9]{2}|100)$/
      if ((!ageStart && ageEnd) || (ageStart && !ageEnd)) {
        this.$message({
          message: '请输入年龄区间',
          type: 'warning',
        })
        return
      } else if (
        (!re.test(ageStart) || !re.test(ageEnd)) &&
        ageStart != '' &&
        ageEnd != ''
      ) {
        this.$message({
          message: '请输入大于0小于100的纯数字',
          type: 'warning',
        })
        return
      } else if (Number(ageStart) > Number(ageEnd)) {
        this.$message({
          message: '年龄最大值不能小于年龄最小值',
          type: 'warning',
        })
        return
      }
      this.$refs.pagination.getTableListData()
    },
    //显示人员详情传参
    toEmployeeDetail(index, row) {
      this.employeeId = row.id
      this.$router.push({
        path: '/home/employee/monitorEmployeeDetail/',
        name: 'monitorEmployeeDetail',
        query: {
          employeeId: this.employeeId,
          name: 'monitorEmployee',
        },
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/employee/monitorEmployee',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
  },
}
</script>

<style lang="less" scoped>
.mainCon {
  top: 198px;
  /deep/ .employeeAge .el-input__inner {
    width: 94px !important;
  }
}
@import '../../../static/css/filterArea.less';
</style>
